package operation;

import book.Book;
import book.BookList;

import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Home-pc
 * Date: 2023-08-17
 * Time: 10:50
 */
public class DelOperation implements IOperation{
    @Override
    public void work(BookList bookList) {
        Scanner sc=new Scanner(System.in);
        System.out.println("请输入要删除的书籍");
        String name=sc.nextLine();
        int index=-1;
        for (int i = 0; i < bookList.getUsedSized(); i++) {
            if(bookList.getBooks(i).getName().equals(name)){//查找有无该书
                index=i;
                break;
            }
        }
        if(index==-1){
            System.out.println("没有你要删除的图书");
            return;
        }
        //找到了，准备开始向前覆盖
        for (int i = index; i < bookList.getUsedSized()-1; i++) {
            Book book=bookList.getBooks(i+1);
            bookList.setBooks(i,book);
        }
        //覆盖之后，最后一个数组位置赋null，并设置书的数量-1
        bookList.setBooks((bookList.getUsedSized())-1,null);
        bookList.setUsedSized(bookList.getUsedSized()-1);
        System.out.println("删除成功");
    }
}
